import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../views/Welcome.vue')
      },
      // 用户管理路由
      {
        path: '/user',
        name: 'User',
        component: () => import('../views/system/User.vue')
      },
      // 个人信息路由
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('../views/user/Profile.vue')
      },
      // 学生管理路由
      {
        path: '/student/management',
        name: 'StudentManagement',
        component: () => import('../views/student/Management.vue')
      },
      // 成绩评分路由
      {
        path: '/academic',
        name: 'Academic',
        component: () => import('../views/score/Academic.vue')
      },
      {
        path: '/political',
        name: 'Political',
        component: () => import('../views/score/Political.vue')
      },
      {
        path: '/research',
        name: 'Research',
        component: () => import('../views/score/Research.vue')
      },
      {
        path: '/social',
        name: 'Social',
        component: () => import('../views/score/Social.vue')
      },
      // 奖学金申请路由
      {
        path: '/application',
        name: 'Application',
        component: () => import('../views/scholarship/Application.vue')
      },
      {
        path: '/review',
        name: 'Review',
        component: () => import('../views/scholarship/Review.vue')
      },
      // 数据统计路由
      {
        path: '/statistics',
        name: 'Statistics',
        component: () => import('../views/statistics/Index.vue')
      },
      // 系统设置路由
      {
        path: '/settings',
        name: 'Settings',
        component: () => import('../views/system/Settings.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router 